/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jhelper.dome;

import java.io.File;
import java.io.FileWriter;
import java.util.Hashtable;
import javax.transaction.TransactionRequiredException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 *
 * @author Administrator
 */
public class AddressBook {
    public boolean newCard(AddressBookBean newcard) throws ParserConfigurationException{
        Document doc;
        Element addressbook,card;
        Element name,mobile = null,office,home,qq,email,other;
        boolean flag=false;
        try{
          
            DocumentBuilderFactory f=DocumentBuilderFactory.newInstance();
            DocumentBuilder db=f.newDocumentBuilder();
            File file=new File("addreebook");
            if(! file.exists()){
                file.mkdir();
            }
            file=new File("addreebook/addressbook.xml");
            if(! file.exists()){
                flag=true;
                file.createNewFile();
                doc=db.newDocument();
                addressbook=doc.createElement("addressbook");
            }else{
                doc=db.parse(file);
                addressbook=(Element)doc.getElementsByTagName("addressbook");
            }
          card=doc.createElement("card");
          name=doc.createElement("name");
          office=doc.createElement("office");
          home=doc.createElement("home");  
          qq=doc.createElement("qq");
          email=doc.createElement("email");  
          other=doc.createElement("other"); 
          
          name.appendChild(doc.createTextNode(newcard.getName()));
          mobile.appendChild(doc.createTextNode(newcard.getMobile()));
          office.appendChild(doc.createTextNode(newcard.getOffice()));
          home.appendChild(doc.createTextNode(newcard.getHome()));
          qq.appendChild(doc.createTextNode(newcard.getQq()));
          email.appendChild(doc.createTextNode(newcard.getEmail()));
          other.appendChild(doc.createTextNode(newcard.getOther()));
          
          
          card.appendChild(name);
          card.appendChild(mobile);
          card.appendChild(office);
          card.appendChild(home);
          card.appendChild(qq);
          card.appendChild(email);
          card.appendChild(other);
          addressbook.appendChild(card);
          
          
          if(flag){
              doc.appendChild(addressbook);
              
          }
          
          TransformerFactory ff=TransformerFactory.newInstance();
          Transformer t=ff.newTransformer();
          t.setOutputProperty("encoding", "GB2312");
          FileWriter fout=new FileWriter(file);
          t.transform(new DOMSource(doc),new StreamResult(fout));
          
          return true;
        }catch(Exception e){
       e.printStackTrace();
       return false;
         
            
        }
       
    } 
    public boolean modifyCard(AddressBookBean aldcard){
        
        return true;
        
    }
    public boolean delCard(String name){
        return true;
        
    }
    public Hashtable getAllCards(){
        return null;
    }
    
}
